#!/bin/bash

# ConnectRPC Calculator - 部署脚本
# 用于构建和部署计算器应用

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 日志函数
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查依赖
check_dependencies() {
    log_info "检查依赖..."
    
    if ! command -v docker &> /dev/null; then
        log_error "Docker 未安装"
        exit 1
    fi
    
    if ! command -v docker-compose &> /dev/null; then
        log_error "Docker Compose 未安装"
        exit 1
    fi
    
    log_info "依赖检查完成"
}

# 清理旧容器和镜像
cleanup() {
    log_info "清理旧容器..."
    docker-compose down --remove-orphans || true
    
    log_info "清理未使用的镜像..."
    docker image prune -f || true
}

# 构建服务
build_services() {
    log_info "构建服务..."
    docker-compose build --no-cache
    log_info "服务构建完成"
}

# 启动服务
start_services() {
    log_info "启动服务..."
    docker-compose up -d
    log_info "服务启动完成"
}

# 检查服务健康状态
check_health() {
    log_info "检查服务健康状态..."
    
    # 等待服务启动
    sleep 10
    
    # 检查后端服务
    max_attempts=30
    attempt=1
    
    while [ $attempt -le $max_attempts ]; do
        if curl -f http://localhost:8090/health > /dev/null 2>&1; then
            log_info "后端服务健康检查通过"
            break
        fi
        
        if [ $attempt -eq $max_attempts ]; then
            log_error "后端服务健康检查失败"
            docker-compose logs calculator-backend
            exit 1
        fi
        
        log_info "等待后端服务启动... ($attempt/$max_attempts)"
        sleep 5
        ((attempt++))
    done
    
    # 检查前端服务
    attempt=1
    while [ $attempt -le $max_attempts ]; do
        if curl -f http://localhost:3000 > /dev/null 2>&1; then
            log_info "前端服务健康检查通过"
            break
        fi
        
        if [ $attempt -eq $max_attempts ]; then
            log_error "前端服务健康检查失败"
            docker-compose logs calculator-frontend
            exit 1
        fi
        
        log_info "等待前端服务启动... ($attempt/$max_attempts)"
        sleep 5
        ((attempt++))
    done
}

# 显示服务状态
show_status() {
    log_info "服务状态:"
    docker-compose ps
    
    echo
    log_info "服务访问地址:"
    echo "  前端应用: http://localhost:3000"
    echo "  后端API:  http://localhost:8090"
    echo "  健康检查: http://localhost:8090/health"
    echo
    log_info "查看日志: docker-compose logs -f"
    log_info "停止服务: docker-compose down"
}

# 主函数
main() {
    case "${1:-deploy}" in
        "build")
            check_dependencies
            build_services
            ;;
        "start")
            start_services
            check_health
            show_status
            ;;
        "stop")
            log_info "停止服务..."
            docker-compose down
            log_info "服务已停止"
            ;;
        "restart")
            log_info "重启服务..."
            docker-compose restart
            check_health
            show_status
            ;;
        "logs")
            docker-compose logs -f
            ;;
        "status")
            docker-compose ps
            ;;
        "clean")
            cleanup
            ;;
        "deploy")
            log_info "开始部署 ConnectRPC 计算器应用..."
            check_dependencies
            cleanup
            build_services
            start_services
            check_health
            show_status
            log_info "部署完成! 🎉"
            ;;
        "help"|"-h"|"--help")
            echo "用法: $0 [命令]"
            echo
            echo "命令:"
            echo "  deploy   完整部署应用 (默认)"
            echo "  build    构建Docker镜像"
            echo "  start    启动服务"
            echo "  stop     停止服务"
            echo "  restart  重启服务"
            echo "  logs     查看服务日志"
            echo "  status   查看服务状态"
            echo "  clean    清理旧容器和镜像"
            echo "  help     显示帮助信息"
            ;;
        *)
            log_error "未知命令: $1"
            echo "使用 '$0 help' 查看可用命令"
            exit 1
            ;;
    esac
}

# 执行主函数
main "$@"